package leetcode;

public class ReverseString {

	public static void main(String[] args) {
		ReverseString object = new ReverseString();
		String s = "abcdefg";
		System.out.println(object.reverseStr(s, 2));
	}
	
	//反转k个字符，并隔k个字符之后再反转下一个k个字符
	public String reverseStr(String s, int k) {
        if(s == null || s.length() <= 0){
        	return s;
        }
        int length = s.length();
        StringBuilder sBuilder = new StringBuilder();
        for(int i = 0; i < length; i += 2 * k){
        	int min = Math.min(i + k - 1, length - 1);
        	for(int j = min; j >= i; j--){
        		sBuilder.append(s.charAt(j));
        	}
        	if(min == length - 1){
        		break;
        	}
        	int nextMin = Math.min(min + k, length - 1);
        	sBuilder.append(s.substring(min + 1, nextMin + 1));
        }
        return sBuilder.toString();
    }
}
